﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevTeam;

using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.DonenRFID
{
    public partial class Analiz : XForm
    {
        public Analiz()
        {
            InitializeComponent();
        }

        BindingSource bsAll, bsCE;
        private void Analiz_Load(object sender, EventArgs e)
        {
            
            //((RepositoryItemDateEdit)de.Edit).ToolTip = "Başlangıç Tarihi";
            //de2.ToolTip = "Bitiş Tarihi";
            de.EditValue = new DateTime(2013, 1, 1);

            bsAll = gvAll.Init("GetDonenRFID",
                de.EditValue.ToDate() == new DateTime(1, 1, 1) ? new DateTime(2001, 1, 1) : de.EditValue.ToDate(),
                de2.EditValue.ToDate() == new DateTime(1, 1, 1) ? new DateTime(2071, 1, 1) : de2.EditValue.ToDate()
                );
            bsCE = gvAll.AddDetailGrid(gvCE, "AtelyeId");
            gvCE.AddDetailGrid(gvTop, "CikisEmriId");

            gvCE.AddColumn("Eksik", typeof(decimal), "CikanAdet-ISNULL(DonenAdet,0)");
            gvCE.AddColumn("DonusPct", typeof(float), "100- 100*Eksik/CikanAdet");
            gvAll.AddColumn("OP", typeof(int), "SUM(Child({FK}).OP)");
            gvAll.AddColumn("Bos", typeof(int), "SUM(Child({FK}).Bos)");
            gvAll.AddColumn("DonenAdet", typeof(int), "SUM(Child({FK}).DonenAdet)");
            gvAll.AddColumn("Eksik", typeof(decimal), "CikanAdet-ISNULL(DonenAdet,0)");
            gvAll.AddColumn("DonusPct", typeof(float), "100- 100*Eksik/CikanAdet");
            gvAll.SetCaptions("DonenAdet:Toplam Dönen");

            gvAll.FormatAsNumber(0, "CikanAdet", "DonenAdet", "OP", "Bos", "Eksik");
            gvCE.FormatAsNumber(0, "CikanAdet", "DonenAdet", "OP", "Bos", "Eksik");
            gvAll.AddSummary("CikanAdet", "DonenAdet", "OP", "Bos", "Eksik");
            gvCE.AddSummary("CikanAdet", "DonenAdet", "OP", "Bos", "Eksik");
            gvTop.AddSummary();

            SetProgress(gvCE.Columns["DonusPct"]);
            SetProgress(gvAll.Columns["DonusPct"]);

            splitContainerControl.AutoSize();
            splitContainerControl2.AutoSize();

            gvCE.MakeColumnInvisible("AtelyeId", "DonenAdet");
            gvTop.MakeColumnInvisible("CikisEmriId");

            bsCE.CurrentChanged += new EventHandler(bsCE_CurrentChanged);

            if (!ytk(179))
                gvAll.MakeColumnInvisible("OP", "Bos");

            gvAll.BestFitColumns();
            gvCE.BestFitColumns();
            gvTop.BestFitColumns();

            btnGo.ItemClick += btnGo_ItemClick;

        }

        void btnGo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAll.EndEditing();
            gvAll.RefreshDataSource(true, "GetDonenRFID",
                de.EditValue.ToDate() == new DateTime(1, 1, 1) ? new DateTime(2001, 1, 1) : de.EditValue.ToDate(),
                de2.EditValue.ToDate() == new DateTime(1, 1, 1) ? new DateTime(2071, 1, 1) : de2.EditValue.ToDate()
                );
        }

        void SetProgress(GridColumn col)
        {
            col.Caption = col.Caption.Replace("Pct", " %");
            col.ColumnEdit = new RepositoryItemProgressBar();
            ((RepositoryItemProgressBar)col.ColumnEdit).ShowTitle = true;
            ((RepositoryItemProgressBar)col.ColumnEdit).PercentView = true;
            ((RepositoryItemProgressBar)col.ColumnEdit).Maximum = 100;
        }

        void bsCE_CurrentChanged(object sender, EventArgs e)
        {
            gvTop.BestFitColumns();
        }
    }
}